<template>
  <div class="component-kfjh actWrap">
    <ul class="ul-li">
      <li>
        <section class="map" v-html="map">
            
        </section>
        <h2>{{housePersons.houseTitle}}</h2>
        <h5>
          <p class="address"><span>{{ housePersons.address}}</span></p>
        </h5>
        <h3>
          <span class="price"><em>￥{{housePersons.rentalPrice}}</em><span class="unit">元/每月</span></span>
        </h3>
        <div class="li-v li-btn clearfix">
          <div class="content">
            <span class="btn btn-gray" @click.stop="contactLandlor">联系房东</span>
            <!--<span class="btn">导航一下</span>-->
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
import houseList from '@/components/houseList'
export default {
  name: 'lookHouseSchedule',
  components: {
    houseList
  },
  data () {
    return {
      index: 0,
      orderList: ['0', '2'],
      housePersons: '',
      map: ''
    }
  },
  created () {
    this.getDatas(0)
  },
  methods: {
    getDatas (i) {
      let id = this.$route.query.id
      this.$axiosPosting(this.$api.getHousePersonsDetailById, {id}).then(res => {
        if (res.code === '0000' && res.data) {
          this.housePersons = res.data
          this.buildMap(res.data)
        } else {
          this.$vux.toast.text(res.msg)
        }
      })
    },
    cancelSchedule (appointId) {
      let _this = this
      let data = {page: 1, pageSize: 10, appointId}
      this.$vux.confirm.show({
        content: '确定取消预约',
        onConfirm: () => {
          _this.$axiosPosting(_this.$api.cancelHouseAppointment, data).then(res => {
            if (res.code === '0000') {
              _this.getDatas(0)
            } else {
              _this.$vux.toast.text('取消预约失败')
            }
          })
        }
      })
    },
    buildMap (item) {
      let {communityName, address, latitude, longitude} = item
      let temp = {
        communityName,
        address,
        latitude,
        longitude
      }
      let map = this.createMap(temp)
      this.map = map
    },
    /* 创建地图 */
    createMap (mapInfo) {
      return `<iframe src="http://apis.map.qq.com/tools/poimarker?type=0&marker=coord:${mapInfo.latitude},${mapInfo.longitude};title:${mapInfo.communityName};addr:${mapInfo.address}&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer=myapp"></iframe>`
    },
    // 联系房东
    contactLandlor () {
      let virtualNum = ''
      let mobileTo = ''
      this.$axiosPosting(this.$api.getUserById, {userId: this.housePersons.userId}).then(res => {
        if (res.code === '0000') {
          mobileTo = res.data.mobile
          step2()
        }
      })
      const step2 = () => {
        let userInfo = JSON.parse(window.localStorage.getItem('userInfo'))
        let mobileFor = userInfo ? userInfo.mobile : ''
        if (mobileFor) {
          if (virtualNum === '') {
            this.$axiosPosting(this.$api.getVirtualNum, {mobileFor, mobileTo}).then(res => {
              let data = ''
              if (res.code === '0000') {
                data = JSON.parse(res.data)
              }
              if (res.code === '0000' && data.errorCode === '0') {
                virtualNum = data.virtualNum
                window.location.href = `tel:${virtualNum}`
              } else {
                this.$vux.toast.text('无法获取房东电话')
                virtualNum = ''
              }
            })
          } else {
            window.location.href = `tel:${virtualNum}`
          }
        } else {
          this.$router.replace('/login')
        }
      }
    }
  }
}
</script>
<style lang="less">
 .scale{
        display:none;
  }
  .component-kfjh {
    .ul-li {
      position: relative;
      li {
        position: relative;
        >h2,>h3,>h4,>h5 ,>div{
          padding: 0 18px;
        }
      }
    }
    .map{
      width:100%;
      height: ~"calc(100vh - 100px)";
      iframe{
        width:100%;
        height:100%;
        border: 0;
        outline: 0;
      }
    }
    h4 {
      height: 35px;
      line-height: 35px;
      position: relative;
    }
    .li-btn {
      text-align: right;
      font-size: 15px;
      border-bottom: 0;
      padding: 0;
      position: absolute;
      bottom: 0;
      right: 0;
    }
    .btn {
      width: 100px;
      height: 28px;
      line-height: 28px;
      background: #ff9600;
      display: inline-block;
      border: 1px solid #ff9600;
      text-align: center;
      color: white;
      border-radius: 2px;
      margin-left: 5px;
      font-size: 14px;
    }
    h2 {
      font-size: 15px;
      font-weight: bold;
      margin-top: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
    h5 {
      font-size: 12px;
      height: 16px;
      line-height: 16px;

    }
    .address {
      position: relative;
    }
    
    .address::before {
      background: url("../../../images/house_list-gps.png") no-repeat;
      position: relative;
      top: 1px;
      font-size: 8px;
    }
    .address::before, .type::before {
      content: '';
      display: inline-block;
      width: 15px;
      height: 12px;
      background-size: contain;
    }
    .price {
      font-size: 13px;
      color: #979797;
    }
    em {
      font-weight: bold;
      font-style: normal;
      font-size: 21px;
      color: #ff3c00;
      display: inline-block;
      margin-right: 2px;
    }
    .unit {
      position: relative;
      bottom: 1px;
    }
    
  }
</style>
